package com.keti.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.keti.Dao.ApplyReceiveDao;
import com.keti.pojo.ApplyReceive;
import com.keti.pojo.User;

import util.DBUtil;

public class ApplyReceiveDaoImpl implements ApplyReceiveDao{
	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	

	@Override
	public void del() {
		
	}

	@Override
	public void updata(ApplyReceive ar) {
		conn = DBUtil.getconn();
		String sql="UPDATE `apply_receive` set uid=?,uname=?,sid=?,sname=?,count=?,status=?,reason=? where id=?";
		try {
			ps = conn.prepareStatement(sql);
			String[] params= {
					String.valueOf(ar.getUid()),
					ar.getUname(),
					String.valueOf(ar.getSid()),
					ar.getSname(),
					String.valueOf(ar.getCount()),
					ar.getStatus(),
					ar.getReason(),
					String.valueOf(ar.getId())
			};
			DBUtil.exeUpd(sql, params);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeAll(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public void add(ApplyReceive ar) {
		conn=DBUtil.getconn();
		String sql="INSERT `apply_receive`(uid,uname,sid,sname,count,status,reason) VALUE(?,?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			String[] params={
					String.valueOf(ar.getUid()),
					ar.getUname(),
					String.valueOf(ar.getSid()),
					ar.getSname(),
					String.valueOf(ar.getCount()),
					ar.getStatus(),
					ar.getReason()
				};
				DBUtil.exeUpd(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeAll(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<ApplyReceive> queryAll() {
		List<ApplyReceive> list = new ArrayList<ApplyReceive>();
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement("select *from `apply_receive`");
			rs = ps.executeQuery();
			while(rs.next()) {
				int id = rs.getInt(1);
				int uid = rs.getInt(2);
				String uname = rs.getString(3);
				int sid = rs.getInt(4);
				String sname = rs.getString(5);
				int count = rs.getInt(6);
				String status = rs.getString(7);
				String reason = rs.getString(8);
				ApplyReceive ar = new ApplyReceive();
				ar.setId(id);
				ar.setUid(uid);
				ar.setUname(uname);
				ar.setSid(sid);
				ar.setSname(sname);
				ar.setCount(count);
				ar.setStatus(status);
				ar.setReason(reason);
			
				list.add(ar);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public ApplyReceive queryOne(int id) {
		conn = DBUtil.getconn();
		ApplyReceive ar = new ApplyReceive();
		try {
			ps = conn.prepareStatement("select *from apply_receive where id=?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next()) {
				id = rs.getInt(1);
				int uid = rs.getInt(2);
				String uname = rs.getString(3);
				int sid = rs.getInt(4);
				String sname = rs.getString(5);
				int count = rs.getInt(6);
				String status = rs.getString(7);
				String reason = rs.getString(8);
				ar.setId(id);
				ar.setUid(uid);
				ar.setUname(uname);
				ar.setSid(sid);
				ar.setSname(sname);
				ar.setCount(count);
				ar.setStatus(status);
				ar.setReason(reason);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeAll(conn, ps, rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ar;
	}

}
